$(function () {
    var dictionary_datagrid = $("#dictionary_datagrid");
    var dictionary_dialog = $("#dictionary_dialog");


    //菜单列表初始化
    dictionary_datagrid.datagrid({
        fit: true,
        url: "/dataDictionary/list",
        fitColumns: true,
        rownumbers: true,
        striped: true,
        toolbar: "#tb",
        singleSelect: true,
        columns: [[
            {field: "id", title: "编号", hidden: true},
            {field: "name", title: "数据名称", width: 100},
            {field: "sn", title: "数据编码", width: 100},
            {field: "intro", title: "数据信息", width: 100}
        ]],
        onClickRow: function (index, row) {
            $("#dataItems_datagrid").datagrid({
                url: '/dataItems/queryItemsBySn?sn=' + row.sn,
                fit: true,
                fitColumns: true,
                columns: [[
                    {field: "id", title: "编号", hidden: true},
                    {field: "name", title: "数据名称", width: 100},
                    {field: "parentSn", title: "数据编码", width: 100},
                    {field: "intro", title: "数据信息", width: 100}
                ]],
                rownumbers: true,
                singleSelect: true,
                toolbar: '#tb2'
            })

        }
    });
    //菜单列表初始化-end

    //编辑或更新对话框
    dictionary_dialog.dialog({
        width: 400,
        height: 500,
        buttons: "#bb",
        closed: true
    });
    //end

});


//响应函数集合对象
var cmdObj = {
    //工具栏的刷新
    reload: function () {
        dictionary_datagrid.datagrid("load");
    },
    //工具栏的编辑
    edit: function () {
        //判断是否选中数据
        var row = dictionary_datagrid.datagrid("getSelected");
        if (!row) {
            $.messager.alert('温馨提示', "请选择要编辑的数据");
            return;
        }
        //添加与编辑时将数据清空
        $("#dictionary_form").form("clear");
        //数据显: 同名原则
        $("#dictionary_form").form("load", row);
        //1:弹框
        dictionary_dialog.dialog("open");
        //2:设置标题
        dictionary_dialog.dialog("setTitle", "菜单编辑");

    },
    //工具栏添加
    add: function () {
        //添加与编辑时将数据清空
        $("#dictionary_form").form("clear");
        //1:弹框
        dictionary_dialog.dialog("open");
        //2:设置标题
        dictionary_dialog.dialog("setTitle", "菜单添加");

    },
    //保存操作
    save: function () {
        var id = $("#dictionaryId").val();
        var url = "/dataDictionary/save";
        if (id) {
            url = "/dataDictionary/update";
        }
        $("#dictionary_form").form('submit', {
            url: url,
            success: function (data) {
                data = $.parseJSON(data);
                if (!data.success) {
                    $.messager.alert('温馨提示', data.msg);
                } else {
                    //关闭框
                    dictionary_dialog.dialog("close");
                    //刷新列表数据
                    dictionary_datagrid.datagrid("load");
                }
            }
        })
    },
    //工具栏删除菜单
    remove: function () {
        //判断是否选中数据
        var row = dictionary_datagrid.datagrid("getSelected");
        if (!row) {
            $.messager.alert('温馨提示', "请选择要编辑的数据");
            return;
        }

        $.messager.confirm('确认', '您确认想要执行这个操作？', function (r) {
            if (r) {
                //1:发起请求修改状态
                $.get("/dataDictionary/delete?id=" + row.id, function (data) {

                    if (data.success) {
                        //2:刷新列表数据
                        dictionary_datagrid.datagrid("load");
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                });
            }
        });
    },
    cancel: function () {
        dictionary_dialog.dialog("close");
    }
};
//end
//调用
$("a[data-cmd]").click(function () {
    var cmd = $(this).data("cmd");
    cmdObj[cmd]();
});



